package My;

public class cyclyQueue {

    public  int [] elem;
    public  int front;
    public  int rear;

    public cyclyQueue(int k) {
        this.elem = new int[k+1];
    }

    public boolean isFull(){
        if((rear+1)%elem.length==front){
            return  true;
        }
        return  false;
    }

    public  boolean offer(int val){
        if(isFull()){
            return false;
        }
        elem[rear]=val;
        rear=(rear+1)%elem.length;
        return true;
    }
    public  boolean poll(){
        if(isempty()){
            return  false;
        }
        //int val=elem[front];
        front=(front+1)%elem.length;
        return true;
    }

    public  int peek(){
        if(isempty()){
            throw new RuntimeException("error");
        }
        return elem[front];
    }
    public  boolean isempty(){
        return front==rear;
    }

    public int rear(){
        if(rear==0){
            return elem[elem.length-1];
        }
        else{
            return elem[rear-1];
        }
    }
}
